package com.example.shoppingmall.home.fragment;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.example.shoppingmall.R;
import com.example.shoppingmall.base.BaseFragment;
import com.example.shoppingmall.home.adapter.HomeFragmentAdatpter;
import com.example.shoppingmall.home.bean.ResultBeanData;
import com.example.shoppingmall.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.w3c.dom.Text;

import okhttp3.Call;

// 作用主页面的Fragment
public class HomeFragment extends BaseFragment {
    private static final String TAG = HomeFragment.class.getSimpleName();
    private TextView textView;
    private RecyclerView rvHome;
    private ImageView ib_top;
    private TextView tv_search_home;
    private TextView tv_message_home;

    // 适配器
    private HomeFragmentAdatpter adatpter;

    // 返回的数据
    private ResultBeanData.ResultBean resultBean;


    @Override
    public View initView() {
        Log.e("Tag","主页面的Fragment的UI被初始化");
        View view = View.inflate(mContext, R.layout.fragment_home,null);
        rvHome = (RecyclerView) view.findViewById(R.id.rv_home);
        ib_top = (ImageView) view.findViewById(R.id.ib_top);
        tv_search_home = (TextView) view.findViewById(R.id.tv_search_home);
        tv_message_home = (TextView) view.findViewById(R.id.tv_message_home);

        // 设置点击事件
        initListener();

        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.e("Tag","主页面的Fragment的数据被初始化");
        //textView.setText("主页面内容");
        getDataFromNet();

    }
    public void getDataFromNet(){
        String url = Constants.HOME_URL;
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        // 请求失败的回调
                        Log.e(TAG,"首页请求失败==" + e.getMessage());
                    }
                    // 请求成功的回调
                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG,"首页请求成功==" + response);

                        // 解析数据
                        processData(response);

                    }
                });
    }

    private void initListener(){
        // 置顶的监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 回到顶部
                rvHome.scrollToPosition(0);
            }
        });
        // 搜索的监听
        tv_search_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "搜索", Toast.LENGTH_SHORT).show();
            }
        });

        // 消息的监听
        tv_message_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "进入消息中心", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void processData(String json){
        ResultBeanData resultBeanData = JSON.parseObject(json,ResultBeanData.class);
        resultBean = resultBeanData.getResult();
        if(resultBean != null){
            // 有数据
            // 设置适配器
            adatpter = new HomeFragmentAdatpter(mContext,resultBean);
            rvHome.setAdapter(adatpter);

            GridLayoutManager manager = new GridLayoutManager(mContext,1);
            // 设置跨度大小监听 设置监听RecyclerView的位置
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if(position <= 4){
                        // 隐藏按钮
                        ib_top.setVisibility(View.GONE);
                    }else {
                        ib_top.setVisibility(View.VISIBLE);
                    }
                    return 1;
                }
            });

            // 设置布局管理者  轮播图
            rvHome.setLayoutManager(manager);

        }else {
            // 没有数据
        }
        //  解析成功 == 现货【一方尘寰】剑侠情缘三剑三七秀 干将莫邪 90橙武仿烧蓝复古对簪
        Log.e("getData","解析成功 == " + resultBean.getHotInfo().get(0).getName());

    }
}
